/**
 * 选择客户js
 */
var selCustomer= {
	curPage:1,
	pageSize:6,
	dataCache:null,
	
	startEvent:function(){
		//按回车键登录事件
		document.onkeydown = function(e){ 
    		var ev = document.all ? window.event : e;
    		if(ev.keyCode==13) {
    			selCustomer.curPage=1;
    			selCustomer.setPageChange(null);
    			selCustomer.search();
     		}
		};
	},
	
	bindEvent:function(){	
		//查询
		$('#searchData').click(function(){
			selCustomer.curPage=1;
			selCustomer.setPageChange(null);
			selCustomer.search();
		});
	},
	
	//设置分页
	setPageChange:function(delCurPage){
		var url = 'common/CustomerCtrl/getCustomerList';
		var name=$.trim($('#searchModForm input[name="name"]').val());
		var identity=$.trim($('#searchModForm input[name="identity"]').val());
		var params={
			'curPage':1,
			'pageSize':99999,
			'name':name,
			'identity':identity
		};
		var cb = function(data) {
			if (data.resultCode == 1) {
				var totalCount = data.size;
				var pageTotal = (totalCount / selCustomer.pageSize > 0) ? Math.ceil(totalCount/ selCustomer.pageSize) : 1;
				pageHelper.init(pageTotal,totalCount,selCustomer.search,delCurPage);  //翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	//查询客户
	search:function(pageIdx){
		var url = 'common/CustomerCtrl/getCustomerList';
		if (typeof(pageIdx) != "undefined"){ 
			selCustomer.curPage=pageIdx;
		}
		var name=$.trim($('#searchModForm input[name="name"]').val());
		var identity=$.trim($('#searchModForm input[name="identity"]').val());
		var params={
			'curPage':selCustomer.curPage,
			'pageSize':selCustomer.pageSize,
			'name':name,
			'identity':identity
		};
		var cb = function(data) {
			if (data.resultCode == 1) {
				$('#tableList tbody').empty();
				var tr='';
				if(data.result!=null&&data.result.length>0){
					var checkedId=art.dialog.data('checkedId');
					selCustomer.dataCache=data.result;
					for(var i=0;i<data.result.length;i++){
						var singleObj=data.result[i];
						tr += '<tr><td>'
							+'<input type="radio" name="id" value="'+singleObj.id+'" '+(singleObj.id==checkedId?'checked="checked"':'')+'/>'
							+ (i+1)
							+ '</td><td>'
							+ singleObj.name
							+ '</td><td>'
							+ (singleObj.type==null?'':singleObj.type)
							+ '</td><td>'
							+ (singleObj.identity==null?'':singleObj.identity)
							+ '</td><td>'
							+ (singleObj.mobile==null?'':singleObj.mobile)
							+ '</td><td>'
							+ (singleObj.phone==null?'':singleObj.phone)
							+ '</td><td>'
							+ (singleObj.occupation==null?'':singleObj.occupation)
							+ '</td></tr>';
					}
				}else{
					tr='<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
				$('#tableList tbody').html(tr);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	init : function() {
		selCustomer.setPageChange(null);
		selCustomer.search();
		selCustomer.startEvent();
		selCustomer.bindEvent();
	}
};

$(document).ready(function() {
	selCustomer.init();
});
function getSelData(){
	var checkedLen=$('#tableList').find('input[name="id"]:checked').length;
	if(checkedLen==0) { 
		alert('至少选择一个客户');
		return false;
	}
	
	var checkId=$('#tableList').find('input[name="id"]:checked').val();
	var ret=[];
	for(var i=0;i<selCustomer.dataCache.length;i++){
		if(selCustomer.dataCache[i].id==checkId){
			ret[0]=checkId;
			ret[1]=selCustomer.dataCache[i].name;
		}
	}
	return ret;
}


